﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace UI.Tools
{
    /// <summary>
    /// API 的摘要说明
    /// </summary>
    public class API : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            WeiXin wx = new WeiXin();
            string area = HttpContext.Current.Request["area"];
            string url = wx.FindUrl(area, "API");
            string result = string.Empty;
            string postParam = string.Empty;               
            using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
            {                
                postParam = sr.ReadLine();
                result = wx.HttpPost(url, postParam);
            }

            #region 把各局服务器上的图片下载到本服务器，解决签名和公章蓝牙打印机不能打印的问题。（需要把局服务器上的公章拷贝到本服务器）
            if ((area == "shangqiu" || area== "zhengyang" || area == "erqi" || area == "zhoukou") && (postParam.Contains("Find_EasyInfo") || postParam.Contains("Find_ParkInfo")))
            {
                //"SignName":"https://shangqiuzhifa.qiyedaishu.com/upload/2024-03-30/145634260.png"
                int index = result.IndexOf("SignName");
                //wx.Log(result);
                string[] list = result.Substring(index, 100).Split('"');
                if (list.Length >= 3)
                {
                    string signname = list[2];
                    string[] names = signname.Split('/');
                    if (names.Length>=2)
                    {
                        string path = "/upload/temp/" + names[names.Length - 1];
                        string root = HttpContext.Current.Server.MapPath(path).Replace(".png", "_500.png");
                        if (!File.Exists(root))
                        {
                            wx.HttpDownload(signname.Replace(".png", "_500.png"), root);
                        }
                        result = result.Replace(signname, "https://tran.qiyedaishu.com" + path).Replace("http://"+area+"zhifa.qiyedaishu.com/img/zhang2.png", "https://tran.qiyedaishu.com/img/" + area + "/zhang2.png").Replace("http://" + area + "zhifa.qiyedaishu.com:12580/img/zhang2.png", "https://tran.qiyedaishu.com/img/" + area + "/zhang2.png");
                    }
                }
                
            }
            #endregion
            context.Response.Write(result);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}